<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:27
 */


namespace app\console\controller;
use app\common\controller\Common;
use app\common\controller\Core;
use app\common\model\Orderqueue;
use app\common\service\AddService;
use app\common\service\OrdinaryService;
use app\common\service\ReceiverService;
use app\user\model\Role as RoleModel;
use my\RedisPackage as redis;
use think\Request;
use think\Response;
use think\exception\HttpResponseException;
use think\Db;

/**
 * 前台公共控制器
 * @package app\index\controller
 */
class Engineer extends Common
{
    /**
     * 处理员工上下班加班状态
     */
    public static function handleWork()
    {
        echo "---------处理班次信息开始---------"."\r\n";
        $time = date('H:i');
        //dump($time);die;
        $data = Db::name('qt_scheduling_type')
            ->where('start_time','<',$time)
            ->where('end_time','>',$time)
            ->find();
        //处理当班技师
        $engineerIds = Db::name('qt_users')->where(['store_id'=>'1','group'=>'1'])->where('work_type',$data['id'])->column('id');
        foreach ($engineerIds as $key=>$value){
            Db::name('qt_users')->where(['id'=>$value])->update(['is_add'=>'1']);
        }
        //处理其他班次技师
        $otherEngineerIds = Db::name('qt_users')->where(['store_id'=>'1','group'=>'1'])->where('work_type','neq',$data['id'])->column('id');
        foreach ($otherEngineerIds as $key=>$value){
            Db::name('qt_users')->where(['id'=>$value])->update(['is_add'=>'2']);
        }
        echo "---------处理班次信息结束---------";
    }



    /**
     * 技师下钟提醒
     */
    public function engineerDown(){
        $store = Db::name('qt_store')->field('id,name,engineer_down_notify_block')->where('status','1')->select();
        echo "----------开始处理".date('Y-m-d H:i:s')."技师下钟提醒----------"."\r\n";
        foreach ($store as $key=>$value){
            echo "----------门店".$value['name']."-开始处理---------"."\r\n";
            if(!empty($value['engineer_down_notify_block'])){
                $time = time() + ($value['engineer_down_notify_block'] * 60);
                $data = Db::name('qt_order_project')->where('end_time','<',$time)->where('message','1')->select();
                if(!empty($data)){
                    foreach ($data as $k=>$v){
                        $msg = [
                            'user_id'=>'0',
                            'to_user_id'=>$v['engineer_id'],
                            'project_id'=>$v['project_id'],
                            'title'=>'您已到钟',
                            'room_id'=>$v['room_id'],
                            'order_id'=>$v['order_id'],
                            'content'=>'',
                            'status'=>'1',
                            'read'=>'0',
                            'type'=>'4',
                            'created_time'=>time(),
                        ];
                        //给楼面发送消息
                        $userRes = Db::name('qt_users')->where(['store_id'=>$value['id'],'group'=>'2'])->select();
                        $userInsert = [];
                        $engineerName = Db::name('qt_users')->where('id',$v['engineer_id'])->value('work_number');
                        foreach ($userRes as $kk=>$vv){
                            $userInsert[$kk] = [
                                'user_id'=>'0',
                                'to_user_id'=>$vv['id'],
                                'project_id'=>$v['project_id'],
                                'title'=>$engineerName.'即将到钟',
                                'room_id'=>$v['room_id'],
                                'order_id'=>$v['order_id'],
                                'content'=>'',
                                'status'=>'1',
                                'read'=>'0',
                                'type'=>'4',
                                'created_time'=>time(),
                            ];
                        }
                        Db::name('qt_message')->insertAll($userInsert);
                        Db::name('qt_message')->insert($msg);
                        Db::name('qt_order_project')->where('id',$v['id'])->update(['message'=>'2']);
                        echo "----------订单".$v['order_id']."-开始处理---------"."\r\n";
                    }
                }
            }
            echo "----------门店".$value['name']."-结束处理---------"."\r\n";
        }
        echo "----------结束".date('Y-m-d H:i:s')."技师下钟提醒----------"."\r\n";
    }


    /**
     * 一键清楚脏数据
     */
    public function clearData(){
        echo "----------开始清理数据---------";
        echo "</br>";
        // 清楚房间脏数据
        echo "----------清理房间数据---------";
        echo "</br>";
        $roomRes = Db::name('qt_room')->where(['store_id'=>'1'])->select();
        foreach ($roomRes as $key=>$value){
            Db::name('qt_room')->where(['id'=>$value['id']])->update(['status_id'=>'1','marge'=>'','down_time'=>null]);
        }
        echo "----------清理用户数据---------";
        echo "</br>";
        // 清楚用户脏数据
        $userRes = Db::name('qt_users')->where(['store_id'=>'1','role'=>'3'])->select();
        foreach ($userRes as $key=>$value){
            Db::name('qt_users')->where(['id'=>$value['id']])->update(['work_status'=>'1']);
        }
        echo "----------清理排位规则数据---------";
        echo "</br>";
        // 清楚排位脏数据
        $userRes = Db::name('qt_order_queue')->where(['store_id'=>'1'])->order('id asc')->select();
        $sort = 1;
        foreach ($userRes as $key=>$value){
            Db::name('qt_order_queue')->where(['id'=>$value['id']])->update([
                'room_id'=>null,
                'project_id'=>null,
                'order_project_id'=>null,
                'order_user_id'=>null,
                'start_time'=>null,
                'end_time'=>null,
                'real_time'=>null,
                'return_time'=>null,
                'return_room'=>null,
                'day'=>null,
                'status'=>'1',
                'sort'=>$sort
            ]);
            $sort ++;
        }
        echo "----------清理项目订单数据---------";
        echo "</br>";
        //清理订单表
        $orderSql="truncate table dp_qt_order";
        //执行sql语句
        Db::execute($orderSql);
        //清理订单子表
        $orderProjectSql="truncate table dp_qt_order_project";
        //执行sql语句
        Db::execute($orderProjectSql);
        echo "----------清理商品订单规则数据---------";
        echo "</br>";
        //清理商品订单表
        $goodsOrderSql="truncate table dp_qt_product_order";
        //执行sql语句
        Db::execute($goodsOrderSql);
        //清理商品订单子表
        $goodsOrderProjectSql="truncate table dp_qt_product_order_item";
        //执行sql语句
        Db::execute($goodsOrderProjectSql);
        echo "----------清理结算数据---------";
        echo "</br>";
        //删除结账单表和项目子表
        $checkOutSql="truncate table dp_qt_check_out";
        //执行sql语句
        Db::execute($checkOutSql);
        $checkOutItemSql="truncate table dp_qt_check_out_item";
        //执行sql语句
        Db::execute($checkOutItemSql);
        //删除支付方式
        $checkOutPaySql="truncate table dp_qt_check_out_pay";
        //执行sql语句
        Db::execute($checkOutPaySql);
        echo "----------清理等位数据---------";
        echo "</br>";
        //删除等位数据
        $waitSql="truncate table dp_qt_wait";
        //执行sql语句
        Db::execute($waitSql);
        echo "----------清理房间预约数据---------";
        echo "</br>";
        //删除房间预订等位数据
        $roomAppSql="truncate table dp_qt_room_appointment";
        //执行sql语句
        Db::execute($roomAppSql);
        echo "----------清理提成信息---------";
        echo "</br>";
        //删除提成信息
        $commissionSql="truncate table dp_qt_commission_log";
        //执行sql语句
        Db::execute($commissionSql);

        echo "----------清理资金使用记录---------";
        echo "</br>";
        //删除提成信息
        $moneyLogSql="truncate table dp_qt_viper_money_log";
        //执行sql语句
        Db::execute($moneyLogSql);

        echo "----------清理积分使用记录---------";
        echo "</br>";
        //删除提成信息
        $integralLogSql="truncate table dp_qt_viper_integral_log";
        //执行sql语句
        Db::execute($integralLogSql);

        echo "----------清理次卡月卡使用记录---------";
        echo "</br>";
        $numberLogSql="truncate table dp_qt_viper_card_use_log";
        //执行sql语句
        Db::execute($numberLogSql);

        echo "----------清理用户次卡月卡数据---------";
        echo "</br>";
        //删除提成信息
        $viperNumberLogSql="truncate table dp_qt_viper_card_item";
        //执行sql语句
        Db::execute($viperNumberLogSql);


        echo "----------清理挂账人员数据---------";
        echo "</br>";
        //删除挂账人员
        $charge="truncate table dp_qt_charge";
        //执行sql语句
        Db::execute($charge);

        echo "----------清理挂账公司数据---------";
        echo "</br>";
        //删除挂账公司
        $chargeCompany="truncate table dp_qt_charge_company";
        //执行sql语句
        Db::execute($chargeCompany);

        echo "----------清理挂账数据---------";
        echo "</br>";
        //删除挂账
        $chargeItem="truncate table dp_qt_charge_item";
        Db::execute($chargeItem);
        echo "----------清理会员充值记录数据---------";
        echo "</br>";
        //删除充值记录
        $delChargeLog="truncate table dp_qt_viper_card_recharge_log";
        //执行sql语句
        Db::execute($delChargeLog);
        echo "----------清理挂账记录数据---------";
        echo "</br>";
        //删除播报信息
        $delVoice="truncate table dp_qt_send_voice_msg";
        //执行sql语句
        Db::execute($delVoice);
        echo "----------删除播报信息数据---------";
        echo "</br>";

        //删除订房信息
        $roomBook="truncate table dp_qt_room_book";
        //执行sql语句
        Db::execute($roomBook);
        echo "----------删除订房信息数据---------";
        echo "</br>";

        //删除手牌发放记录
        $handGrant="truncate table dp_qt_hand_grant";
        //执行sql语句
        Db::execute($handGrant);
        echo "----------删除手牌发放记录数据---------";
        echo "</br>";
        $handCardRes = Db::name('qt_hand_card')->where(['store_id'=>'1'])->order('id asc')->select();
        foreach ($handCardRes as $key=>$value){
            Db::name('qt_hand_card')->where(['id'=>$value['id']])->update(['status'=>'1']);
        }
        echo "----------充值手牌数据---------";
        echo "</br>";

        echo "----------删除下单明细---------";
        echo "</br>";
        //删除下单明细
        $handGrant="truncate table dp_qt_order_detail";
        //执行sql语句
        Db::execute($handGrant);

        echo "----------删除会员卡---------";
        echo "</br>";
        //删除会员卡
        $viperCard="truncate table dp_qt_viper_card";
        //执行sql语句
        Db::execute($viperCard);


        echo "----------删除会员卡返还记录---------";
        echo "</br>";
        //删除会员卡返回记录
        $viperCardReturn="truncate table dp_qt_viper_card_return";
        //执行sql语句
        Db::execute($viperCardReturn);


        echo "----------删除会员卡转账记录---------";
        echo "</br>";
        //删除会员卡转账记录
        $viperCardGiro="truncate table dp_qt_viper_card_giro";
        //执行sql语句
        Db::execute($viperCardGiro);


        echo "----------清除消息记录---------";
        echo "</br>";
        //删除消息记录
        $message="truncate table dp_qt_message";
        //执行sql语句
        Db::execute($message);

        echo "---------处理班次信息开始---------";
        echo "</br>";
        $time = date('H:i');
        //dump($time);die;
        $data = Db::name('qt_scheduling_type')
            ->where('start_time','<',$time)
            ->where('end_time','>',$time)
            ->find();
        //处理当班技师
        $engineerIds = Db::name('qt_users')->where(['store_id'=>'1','group'=>'1'])->where('work_type',$data['id'])->column('id');
        foreach ($engineerIds as $key=>$value){
            Db::name('qt_users')->where(['id'=>$value])->update(['work_status'=>'1','is_add'=>'1']);
        }
        //处理其他班次技师
        $otherEngineerIds = Db::name('qt_users')->where(['store_id'=>'1','group'=>'1'])->where('work_type','neq',$data['id'])->column('id');
        foreach ($otherEngineerIds as $key=>$value){
            Db::name('qt_users')->where(['id'=>$value])->update(['work_status'=>'2','is_add'=>'2']);
        }
        echo "---------处理班次信息结束---------";
        echo "</br>";
        echo "---------删除会员补录信息----------";
        echo "</br>";
        //删除会员补录信息
        $viperCardSupp="truncate table dp_qt_viper_card_supplement";
        //执行sql语句
        Db::execute($viperCardSupp);

        echo "----------删除净房数据记录---------";
        echo "</br>";
        //删除会员卡转账记录
        $roomClear="truncate table dp_qt_room_clear";
        //执行sql语句
        Db::execute($roomClear);



        echo "----------结束清理数据---------";
        echo "</br>";
    }


    /**
     * 定时处理房间预约
     */
    public function appointment(){
        //超时的预约信息
        $data = Db::name('qt_room_appointment')->field('time + (delay * 60) as total_time,id')->where('status','1')->select();
        foreach ($data as $key=>$value){
            if(time() > $value['total_time']){
                //更改预约状态
                Db::name('qt_room_appointment')->where('id',$value['id'])->update(['status'=>'3']);
                $roomStatus = Db::name('qt_room')->where(['id'=>$value['room_id']])->value('status_id');
                if($roomStatus != null && $roomStatus == '9'){
                    Db::name('qt_room')->where('id',$value['room_id'])->update(['status_id'=>'1']);
                }
            }
        }
    }


    /**
     * 处理单个门店技师按照合并班次排班
     * @param $storeId [门店id ]
     */
    public function storeQueue(){
        $storeId = '1';
        $storeRule = storeQueue($storeId);
        $storeRes = Db::name('qt_store')->where(['id'=>$storeId])->find();
        $storeTime = strtotime(date('Y-m-d').' '.$storeRes['start_time']);
        if(time() > $storeTime){
            if($storeRule['rule'] == '2' ){
                //查询出所有技师排序
                $engineerData = Db::name('qt_order_queue')
                    ->field('oq.id,oq.sort,u.work_type,oq.user_id')
                    ->alias('oq')
                    ->leftJoin('qt_users u','u.id = oq.user_id')
                    ->where(['oq.store_id'=>$storeId,'is_reset'=>'1'])
                    ->select();
                $i = 0;
                $j = 0;
                $useData = [];
                $notUseData = [];
                //处理未排班的技师
                foreach ($engineerData as $key=>$value){
                    if($value['work_type'] == '0'){
                        $notUseData[$j]['user_id'] = $value['user_id'];
                        unset($engineerData[$key]);
                        $j ++;
                    }
                }
                //查询出所有班次
                $workTypeData = Db::name('qt_scheduling_type')->field('id,sort')->where(['store_id'=>$storeId])->order('sort asc')->select();
                foreach ($workTypeData as $key=>$value){
                    foreach ($engineerData as $k=>$v) {
                        if($v['work_type'] == $value['sort'] && $v['work_type'] != '0'){
                            $useData[$i]['user_id'] = $v['user_id'];
                            $useData[$i]['sort'] = $i + 1;
                            $i ++;
                        }
                    }
                }
                foreach ($notUseData as $key =>$value){
                    $notUseData[$key]['sort'] = $i + 1;
                    $i++;
                }
                $data = array_merge($useData,$notUseData);
                foreach ($data as $key=>$value){
                    Db::name('qt_order_queue')->where(['user_id'=>$value['user_id']])->update(['sort'=>$value['sort'],'is_reset'=>'2']);
                }
            }
        }

    }


    /**
     * 按照排钟多少来排序
     */
    public function rowSort(){
        $storeId = '1';
        $storeRule = storeQueue($storeId);
        $storeRes = Db::name('qt_store')->where(['id'=>$storeId])->find();
        $storeTime = strtotime(date('Y-m-d').' '.$storeRes['start_time']);
        if(time() > $storeTime){
            if($storeRule['rule'] == '4'){
                //查询
                $data = Db::name('qt_order_project')
                    ->alias('op')
                    ->leftJoin('qt_order_queue oq','oq.user_id = op.engineer_id')
                    ->field('op.id,sum(num) as row_number,op.engineer_id as user_id')
                    ->where(['op.store_id'=>$storeId,'oq.is_reset'=>'1'])
                    ->whereNotNull('op.engineer_id')
                    ->where('op.engineer_id','<>','0')
                    ->group('op.engineer_id')
                    ->order('row_number asc oq.sort asc')
                    ->select();
                //已经上钟的技师
                $rowEngineer = [];
                foreach ($data as $key=>$value){
                    $rowEngineer[$key] = $value['user_id'];
                }
                $rowEngineer = array_values($rowEngineer);
                //查询出所有技师排序
                $engineerData = Db::name('qt_order_queue')
                    ->field('oq.id,oq.sort,u.work_type,oq.user_id')
                    ->alias('oq')
                    ->leftJoin('qt_users u','u.id = oq.user_id')
                    ->where(['oq.store_id'=>$storeId,'oq.is_reset'=>'1'])
                    ->order('oq.sort asc')
                    ->select();
                foreach ($engineerData as $key=>$value){
                    if(in_array($value['user_id'],$rowEngineer)){
                        unset($engineerData[$key]);
                    }
                }
                $i = 0;
                $j = 0;
                $useData = [];
                $notUseData = [];
                //处理没有排钟技师
                foreach ($engineerData as $key=>$value){
                    $notUseData[$j]['user_id'] = $value['user_id'];
                    $notUseData[$j]['sort'] = $j + 1;
                    $j ++;
                }
                foreach ($data as $key=>$value){
                    $useData[$i]['user_id'] = $value['user_id'];
                    $useData[$i]['sort'] = $j + 1;
                    $j++;
                    $i++;
                }
                $sortData = array_merge($notUseData,$useData);
                foreach ($sortData as $key=>$value){
                    Db::name('qt_order_queue')->where(['user_id'=>$value['user_id']])->update(['sort'=>$value['sort'],'oq.is_reset'=>'2']);
                }
            }
        }
    }


    /**
     * 处理上钟点排
     */
    public function clockSort(){
        $storeId = '1';
        $storeRes = Db::name('qt_store')->where(['id'=>$storeId])->find();
        $storeRule = Db::name('qt_queue_rule')->where(['id'=>$storeRes['queue_rule_id']])->find();
        //排钟规则为上钟点排
        if($storeRule['type' == '2']){
            $storeTime = strtotime(date('Y-m-d').' '.$storeRes['start_time']);
            if(time() > $storeTime){
                if($storeRes['rule'] == '4'){
                    //查询
                    $data = Db::name('qt_order_project')
                        ->alias('op')
                        ->leftJoin('qt_order_queue oq','oq.user_id = op.engineer_id')
                        ->field('op.id,sum(num) as row_number,op.engineer_id as user_id')
                        ->where(['op.store_id'=>$storeId,'oq.is_reset'=>'1'])
                        ->whereNotNull('op.engineer_id')
                        ->where('op.engineer_id','<>','0')
                        ->group('op.engineer_id')
                        ->order('row_number asc oq.sort asc')
                        ->select();
                    //已经上钟的技师
                    $rowEngineer = [];
                    foreach ($data as $key=>$value){
                        $rowEngineer[$key] = $value['user_id'];
                    }
                    $rowEngineer = array_values($rowEngineer);
                    //查询出所有技师排序
                    $engineerData = Db::name('qt_order_queue')
                        ->field('oq.id,oq.sort,u.work_type,oq.user_id')
                        ->alias('oq')
                        ->leftJoin('qt_users u','u.id = oq.user_id')
                        ->where(['oq.store_id'=>$storeId,'oq.is_reset'=>'1'])
                        ->order('oq.sort asc')
                        ->select();
                    foreach ($engineerData as $key=>$value){
                        if(in_array($value['user_id'],$rowEngineer)){
                            unset($engineerData[$key]);
                        }
                    }
                    $i = 0;
                    $j = 0;
                    $useData = [];
                    $notUseData = [];
                    //处理没有排钟技师
                    foreach ($engineerData as $key=>$value){
                        $notUseData[$j]['user_id'] = $value['user_id'];
                        $notUseData[$j]['sort'] = $j + 1;
                        $j ++;
                    }
                    foreach ($data as $key=>$value){
                        $useData[$i]['user_id'] = $value['user_id'];
                        $useData[$i]['sort'] = $j + 1;
                        $j++;
                        $i++;
                    }
                    $sortData = array_merge($notUseData,$useData);
                    foreach ($sortData as $key=>$value){
                        Db::name('qt_order_queue')->where(['user_id'=>$value['user_id']])->update(['sort'=>$value['sort'],'oq.is_reset'=>'2']);
                    }
                }
            }
        }



    }




    /**
     * 超时自动算加钟
     */
    public function addClock(){
        $storeInfo = Db::name('qt_store')->where(['id'=>'1'])->find();
        if($storeInfo['is_delay_add_block'] == '1'){
            echo "---------超时自动加钟开始---------"."\r\n";
            //查找所有超时的订单
            $projectOrder = Db::name('qt_order_project')
                ->where(['store_id'=>'1','status'=>'2','is_add_clock'=>'2'])
                ->where('end_time','<',time())
                ->select();
            foreach ($projectOrder as $key=>$value){
                //增加技师下钟超时算加钟
                $users = Db::name('qt_users')->where(['id'=>$value['engineer_id']])->find();
                if($users['overtime'] != '0'){
                    echo "----------订单【".$value['id'].$value['name']."】开始处理---------"."\r\n";
                    //计算是否加钟
                    $time = ceil((time() - $value['end_time']) / 60);
                    if($time > $users['overtime']){
                        $project = Db::name('qt_project')->where('id',$value['project_id'])->find();
                        //下新项目
                        $insertOrderProject = [
                            'store_id'=>$value['store_id'],
                            'name'=>$value['name'],
                            'created_order_id'=>$value['created_order_id'],
                            'order_id'=>$value['order_id'],
                            'engineer_id'=>$value['engineer_id'],
                            'project_id'=>$value['project_id'],
                            'room_id'=>$value['room_id'],
                            'hand_card_id'=>$value['hand_card_id'],
                            'old_card_id'=>$value['old_card_id'],
                            'order_type'=>'5',
                            'num'=>'1',
                            'price'=>$project['add_block_price'],
                            'status'=>2,
                            'is_add'=>$users['is_add'],
                            'start_time'=>time(),
                            'end_time'=>$value['end_time'] + ($project['add_block_time'] * 60 ),
                            'is_change'=>'1',
                            'created_time'=>time(),
                            'creater_room'=>$value['creater_room'],
                        ];
                        //更新项目订单状态
                        Db::name('qt_order_project')->where('id',$value['id'])->update(['end_time'=>$value['end_time'] + ($project['add_block_time'] * 60 ),'status'=>'2','is_add_clock'=>'1']);
                        $orderProjectId = Db::name('qt_order_project')->insertGetId($insertOrderProject);
                        Db::name('qt_order_queue')->where('user_id',$value['engineer_id'])->update(['end_time'=>$value['end_time'] + ($project['add_block_time'] * 60 )]);
                        //更新提成信息
                        $project = Db::name('qt_project')->where('id', $value['project_id'])->find();
                        $commission = 0;
                        $isAdd = Db::name('qt_users')->where(['id'=>$value['engineer_id']])->value('is_add');
                        //普通提成
                        if ($isAdd == '1') {
                            $commission = new OrdinaryService();
                            $commission = $commission->showCommission('5', $isAdd, $project);
                        }
                        //加班提成
                        if ($isAdd == '2') {
                            $commission = new AddService();
                            $commission = $commission->showCommission('5', $isAdd, $project);
                        }
                        //计算接待提成
                        $receiver = new ReceiverService();
                        $receiver = $receiver->showCommission('5', $isAdd, $project);
                        $floor_commission_type = Db::name('qt_store')->where(['id'=>$value['store_id']])->value('floor_commission_type');
                        if ($floor_commission_type == '1') {
                            $cashierId = $value['created_order_id'];
                        } else {
                            $cashierId = $value['creater_room'];
                        }
                        $insert = [
                            'store_id' => $value['store_id'],
                            'cashier_id' => $cashierId,//接待人
                            'user_id' => $value['engineer_id'],//技师
                            'room_id' => $value['room_id'],
                            'money' =>$project['add_block_price'],
                            'commission' => $commission,//技师提成
                            'receiver' => $receiver,//接待提成
                            'product_id' => $value['project_id'],
                            'type' => '1',
                            'created_time' => time(),
                            'is_pay' => '1',
                            'order_project_id' => $orderProjectId,
                            'status' => '5',
                            'is_add' => $isAdd,
                        ];
                        Db::name('qt_commission_log')->insert($insert);
                        //更新订单提成
                        Db::name('qt_order_project')->where(['id'=>$orderProjectId])->update(['commission'=>$commission,'receiver'=>$receiver]);
                    }
                }
                echo "----------订单【".$value['id'].$value['name']."】处理结束---------"."\r\n";
            }
            echo "---------超时自动加钟结束---------"."\r\n";
        }else{
            echo "---------超时自动已经关闭---------";
        }

    }

    /**
     * 自动安排延迟排钟
     */
    public function wait(){
        echo "---------处理延时排钟开始---------"."\r\n";
        $waitResData = Db::name('qt_wait')
            ->where([ 'status' => '1'])
            ->where('delay_time','neq',0)
            ->order('is_first desc id desc')
            ->select();
        if(empty($waitResData)){
            echo "---------延时排钟数据为空---------"."\r\n";
        }else{
            foreach ($waitResData as $key=>$value){
                $time = (time() - $value['create_time']) / 60;
                $time = ceil($time);
                if($time > $value['delay_time']){
                    $userInfo = Db::name('qt_users')->where(['id'=>$value['create_id']])->find();
                    //安排等位
                    $waitRes = Db::name('qt_wait')
                        ->where(['id' => $value['id']])
                        ->find();
                    if (!empty($waitRes)) {
                        echo "---------处理".$waitRes['id']."延时排钟开始---------"."\r\n";
                        //查找可以安排的技师
                        $data['man_num'] = 0;
                        $data['woman_num'] = 0;
                        $data['other_num'] = 0;
                        if($waitRes['sex'] == '1'){
                            $data['man_num'] = $waitRes['number'];
                        }
                        if($waitRes['sex'] == '2'){
                            $data['woman_num'] = $waitRes['number'];
                        }
                        if($waitRes['sex'] == '3'){
                            $data['other_num'] = $waitRes['number'];
                        }
                        $data['project_id'] = $waitRes['project_id'];
                        $data['room_id'] = $waitRes['room_id'];
                        $data['hand_card_id'] = '';
                        $data['delay']= 0;
                        $data['time_type'] = 1;
                        $data['order_type'] = 1;
                        $data['create_id'] = $userInfo['id']?? '';
                        $orderModel = new \app\common\model\Order();
                        $code = $orderModel->create_order($data,$userInfo);
                        if($code['code'] == '200'){
                            Db::name('qt_wait')->where(['id' => $waitRes['id']])->update(['status' => '2']);
                            echo "---------处理".$waitRes['id']."延时排钟成功---------"."\r\n";
                        }else{
                            echo "---------处理".$waitRes['id']."延时排钟失败---------"."\r\n";
                        }
                    }
                }
            }
        }
        echo "---------处理延时排钟结束---------"."\r\n";

    }


}

